# coding=utf-8
"""
Copyright 2019-2020 Fang Jin Biao

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

# @Time    : 2020/7/19 19:35
# @Author  : Fang Jin Biao
# @Email   : 215466914@qq.com
# @File    : knowledge_acquire_api.py
# @Project : SnowAdminWeb
import datetime

from flask import Blueprint, request, session,g
from flask_restful import Api, Resource, marshal_with
from flask_restful.reqparse import RequestParser

from webapp.config.api_utils import build_params, oper_log, check_auth
from webapp.dto_mapper import user_extraction_page_fields,user_extraction_fields
from webapp.schema import UserExtractionSchema
from webapp.service.user_extraction_service import UserExtractionService
from webapp.tools import format_date, download_excel_knowledge, connect_server
from collections import OrderedDict
import json
user_extraction_app = Blueprint('user_extraction', __name__, url_prefix='/api/UserExtraction')
user_extraction_api = Api(user_extraction_app)

parser = RequestParser()
parser.add_argument('id', location='json', type=int, trim=True, required=True)



@user_extraction_api.resource('', '/')
class AppAPI(Resource):
    @oper_log('查询知识提取', request)
    @marshal_with(user_extraction_page_fields)
    def get(self):
        params = request.args.items(multi=True)
        params_dict = build_params(params)
        data = json.loads(g.get("user"))
        user_id = data["id"]
        pagination = UserExtractionService.query_all(user_id=user_id,**params_dict)
        return pagination

    @oper_log('新增知识提取', request)
    def post(self):
        parser_post = parser.copy()
        parser_post.remove_argument('id')
        parser_post.parse_args()
        params = request.get_json()
        schema = UserExtractionSchema()
        curr_data = schema.load(params, unknown="exclude")
        data = json.loads(g.get("user"))
        curr_data.user_id = data["id"]
        return UserExtractionService.create(curr_data=curr_data)

    @oper_log('修改知识提取', request)
    def put(self):
        parser_post = parser.copy()
        parser_post.parse_args()
        params = request.get_json()
        schema = UserExtractionSchema()
        curr_data = schema.load(params, unknown="exclude")
        data = json.loads(g.get("user"))
        curr_data.user_id = data["id"]
        UserExtractionService.update(curr_data=curr_data)

    @oper_log('删除知识提取', request)
    def delete(self):
        ids = request.get_json()
        UserExtractionService.delete(ids=ids)

@user_extraction_api.resource('/<int:_id>')
class GetServerById(Resource):
    @oper_log('根据ID查询知识提取', request)
    @marshal_with(user_extraction_fields)
    def get(self, _id):
        data = json.loads(g.get("user"))
        user_id = data["id"]
        data = UserExtractionService.find_by_id(id=_id,user_id=user_id)
        return data


